package day_2022_7_to_8.day48;

import java.util.concurrent.ThreadLocalRandom;

/**
 * @author haomin
 * @date 2022/07/30 09:51
 **/
public class Day15_1_Num1833_MaxIceCream {
    private static final ThreadLocalRandom random = ThreadLocalRandom.current();
    public static void main(String[] args) {
        int[] arr = new int[4096];
        for (int i = 0; i < arr.length; i++) {
            //arr[i] = (int) (Math.random()*99);
            arr[i] = random.nextInt(1,10);
        }
        int[] arr1 = arr.clone();
        int[] arr2 = arr.clone();


        long start = System.nanoTime();
        InsertSort(arr);
        long end = System.nanoTime();
        System.out.println("插入排序：耗时："+(end-start)/1000000.0 + "ms");


        start = System.nanoTime();
        BubbleSort(arr1);
        end = System.nanoTime();
        System.out.println("冒泡排序：耗时："+(end-start)/1000000.0 + "ms");



        start = System.nanoTime();
        SelectSort(arr2);
        end = System.nanoTime();
        System.out.println("选择排序：耗时："+(end-start)/1000000.0 + "ms");

    }

    public int maxIceCream(int[] costs, int coins) {
        InsertSort(costs);
        int count = 0;
        for(int i = 0;i < costs.length;i++){
            if(coins >= costs[i]){
                coins-=costs[i];
                count++;
            }else break;
        }
        return count;
    }

    // 交换三联
    public static void swap(int[] arr, int i, int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    // 插入排序
    public static void InsertSort(int[] arr){
        for(int i = 0;i < arr.length;i++){
            for(int j = i;j>0 && arr[j] < arr[j-1];j--){
                swap(arr,j,j-1);
            }
        }

    }

    // 冒泡
    public static void BubbleSort(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length-i-1; j++) {
                if(arr[j] > arr[j+1]) swap(arr, j, j+1);
            }
        }
    }

    // 选择排序
    public static void SelectSort(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            int min = i;
            for (int j = i+1; j < arr.length; j++) {
                if(arr[min] > arr[j]) min = j;
            }
            swap(arr, i, min);
        }
    }
}

